<?php

namespace app\admin\controller;

use app\admin\validate\AdminMenuValidate;
use app\company\model\CompanyMenu as CompanyMenuModel;
use app\company\validate\CompanyMenuValidate;
use app\service\traits\Crud;
use app\admin\model\AdminMenu as AdminMenuModel;
use app\service\traits\Menu;
use think\Response;

class AdminMenu extends Base
{
    private AdminMenuModel $model;

    use Crud;
    use Menu;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new AdminMenuModel();
        $this->validateClass = AdminMenuValidate::class;
        $this->field         = 'id,name,status,path,order_num,parent_id,menu_type,perms,component,query,icon,visible';
        $this->withSearch    = 'name,status';
        $this->append        = 'menu_type_text,status_text';
    }


    public function list(): Response
    {
        $param = request()->param();
        $sql   = $this->model->withSearch($this->withSearch, $param);
        if ($this->field) $sql = $sql->field($this->field);
        $list = $sql->select();
        if (!$list->isEmpty()) $list = $this->getTreeData($list->append(explode(',', $this->append))->toArray());
        return $this->success('获取成功', compact('list'));
    }

}